package com.hdoit.pisces.web.user.controllers;

//import java.util.List;

import java.util.List;

import javax.annotation.Resource;

import org.activiti.engine.impl.cmd.GetModelCmd;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.druid.util.StringUtils;
import com.tennetcn.common.enums.ManageContainer;
import com.tennetcn.common.enums.ModelStatus;
import com.tennetcn.common.enums.pub.SexStatus;
import com.tennetcn.common.enums.pub.YesOrNo;
import com.tennetcn.common.enums.pub.YesOrNoInteger;
import com.tennetcn.common.logical.superbase.model.Employee;
import com.tennetcn.common.logical.superbase.service.IEmployeeService;
import com.tennetcn.common.logical.viewmodel.superbase.EmployeeSearch;
import com.tennetcn.common.logical.viewmodel.superbase.UpdatePwd;
import com.tennetcn.common.message.PagerModel;
import com.tennetcn.common.utils.ChineseUtils;
import com.tennetcn.common.utils.CommonUtils;
import com.tennetcn.common.utils.DateUtils;
import com.tennetcn.common.utils.MD5Utils;
import com.tennetcn.component.web.ActionResult;
import com.tennetcn.component.web.BaseController;
import com.tennetcn.component.web.ErrorResult;
//import com.tennetcn.component.web.SessionWebFactory;
//import com.tennetcn.web.superbase.controllers.EmployeeController;

@Controller
@RequestMapping(value="/admin/user/personal/")
public class PersonalController extends BaseController{
	
	@Override
	public String getRoutePath(){
		return "areas/admin/user/personal/";
	}
	
	
	private IEmployeeService employeeService;
	
	public IEmployeeService getEmployeeService() {
		return employeeService;
	}

	@Resource
	public void setEmployeeService(IEmployeeService employeeService) {
		this.employeeService = employeeService;
	}
	
	private final String indexViewName = "index";
	private final String outputViewName = "output";
	private final String employeeDetailedViewName = "employeeDetailed";
	private final String saveViewName = "save";
	private final String checkCodeViewName = "checkCode";
	private final String checkAccountViewName = "checkAccount";
	private final String personalInformationViewName = "personalInformation";
	private final String updatePwdViewName = "updatePwd";
	
	@RequestMapping(value = indexViewName, method = {RequestMethod.GET,RequestMethod.POST })
	public String index(Model model,PagerModel pagerModel,EmployeeSearch search,UpdatePwd updatePwd,boolean ajaxPost) {
	
		String key = getLoginModel().getLoginId();
	//	UpdatePwd updatePwd=new UpdatePwd();
		
		//Employee employee=employeeService.queryModel(key);
//		updatePwd.setEmployeeId(getLoginModel().getLoginId());
//		updatePwd.setOldPwd(MD5Utils.encoderByMd5With32Bit(updatePwd.getOldPwd()));
		Employee employee = employeeService.queryModel(key);
//		Employee employeeP=employeeService.loginById(updatePwd.getEmployeeId(), updatePwd.getOldPwd());
//		ActionResult actionResult;
//		if(employeeP==null){
//			actionResult=new ErrorResult();
//			actionResult.setMessage("密码错误");
//		}else{
//			if(employeeService.updatePwd(updatePwd)){
//				actionResult=new ActionResult();
//				actionResult.setRefresh(false);
//				sessionWebFactory.clearCurrentLogin();
//			}else{
//				actionResult=new ErrorResult();
//			}
//		}
		
		//System.out.println(getLoginModel().getLoginId()+"22222222222");
		//System.out.print(key+"1111111111111111");
		//System.out.print(employees.size());
		//model.addAttribute("sessionWebFactory", sessionWebFactory);
		//model.addAttribute("updatePwd", updatePwd);
		model.addAttribute("employee", employee);
		//model.addAttribute(ManageContainer.RESULTKEY,actionResult);
//		System.out.println("111111111111111111");
//		System.out.println(employees.get(0).getEmployeeName());
//		System.out.println(employees.get(0).getEmployeeAccount());
//		System.out.println(employees.get(0).getEmail());
		//model.addAttribute("employeeSearch",search);
	//	model.addAttribute(ManageContainer.PAGERKEY, pagerModel);
		

		/*if(ajaxPost){
			
			return view(outputViewName);
		}*/
		return view(indexViewName);
	}

	
	@RequestMapping(value = employeeDetailedViewName, method = { RequestMethod.GET})
	public String employeeDetailed(Model model,String employeeId) {
		Employee employee=new Employee();
		employee.setSex(SexStatus.male);
		if(!StringUtils.isEmpty(employeeId)){
			employee=employeeService.queryModel(employeeId);
		}
		model.addAttribute("employee", employee);
		
		return view(employeeDetailedViewName);
	}
	
	
	@RequestMapping(value = saveViewName, method = { RequestMethod.POST })
	public String save(Model model,Employee employee,UpdatePwd updatePwd) {
		 if(StringUtils.isEmpty(employee.getEmployeeId())){
			 //新增
			 employee.setEmployeeId(CommonUtils.getKeyId());
			 employee.setSex("3");
			// System.out.println("11111111111110"+employee.getSex());
			 employee.setCreateDate(DateUtils.getCurrent());
			 employee.setCreateUserId(sessionWebFactory.getCurrentLogin().getLoginId());
			 employee.setCreateUserName(sessionWebFactory.getCurrentLogin().getLoginName());
			 employee.setDeleteMark(Integer.parseInt(YesOrNo.no));
			 employee.setIsLocked(YesOrNoInteger.no);
			 employee.setIsLogin(YesOrNoInteger.yes);
			 employee.setModelStatus(ModelStatus.add);
		 }else{
			 employee.setModifyDate(DateUtils.getCurrent());
			 employee.setModifyUserId(sessionWebFactory.getCurrentLogin().getLoginId());
			 employee.setModifyUserName(sessionWebFactory.getCurrentLogin().getLoginName());
			 employee.setModelStatus(ModelStatus.update);
			 
			// System.out.println("1111111111111111111111111111111");
			// System.out.println(employee.getRoleIds());
		 }
		 //markcode
		 employee.setMarkCode(ChineseUtils.getPinYinHeadChar(employee.getEmployeeName()));
		 
		
		ActionResult actionResult;
		if(employeeService.applyChange(employee)){
			actionResult=new ActionResult();
		}else{
			actionResult=new ErrorResult();
		}
		//model.addAttribute("updatePwd", updatePwd);
		model.addAttribute("employee", employee);
		model.addAttribute(ManageContainer.RESULTKEY,actionResult);
		
		return view(indexViewName);
	}
	
	
	@RequestMapping(value = checkCodeViewName, method = { RequestMethod.GET })
	public @ResponseBody String checkCode(String employeeCode,String employeeId){
		return String.valueOf(!employeeService.checkCode(employeeId, employeeCode));
	}
	
	
	@RequestMapping(value = checkAccountViewName, method = { RequestMethod.GET })
	public @ResponseBody String checkAccount(String employeeAccount,String employeeId){
		return String.valueOf(!employeeService.checkAccount(employeeId, employeeAccount));
	}
	
	@RequestMapping(value = personalInformationViewName, method = { RequestMethod.GET })
	public String personalInformation(String employeeCode,String employeeId){
		return view(personalInformationViewName);
	}
	
	@RequestMapping(value = updatePwdViewName, method = { RequestMethod.GET })
	public String updatePwd(Model model){
		UpdatePwd pwd=new UpdatePwd();
		model.addAttribute("updatePwd", pwd);
		
		return view(updatePwdViewName);
	}
	
	@RequestMapping(value = updatePwdViewName, method = { RequestMethod.POST })
	public String updatePwd(Model model,UpdatePwd updatePwd){
		updatePwd.setEmployeeId(getLoginModel().getLoginId());
		updatePwd.setOldPwd(MD5Utils.encoderByMd5With32Bit(updatePwd.getOldPwd()));
		
		Employee employee=employeeService.loginById(updatePwd.getEmployeeId(), updatePwd.getOldPwd());
		ActionResult actionResult;
		if(employee==null){
			actionResult=new ErrorResult();
			actionResult.setMessage("密码错误");
		}else{
			if(employeeService.updatePwd(updatePwd)){
				actionResult=new ActionResult();
				actionResult.setRefresh(false);
				sessionWebFactory.clearCurrentLogin();
			}else{
				actionResult=new ErrorResult();
			}
		}
		model.addAttribute(ManageContainer.RESULTKEY,actionResult);
		
		return view(updatePwdViewName);
	}
	
	

}
